#include <Editor/StdAfx.h>
#include "DotEditorPage.h"
#include "wxIDs.h"

BEGIN_EVENT_TABLE(DotEditorPage, wxPanel)
	EVT_BUTTON(ID_BUTTON_DOT_CHANGE_PROPERTY, OnButtonChangeProperty)
	EVT_BUTTON(ID_BUTTON_DOT_CREATE_CHARACTER, OnButtonCharacter)
	EVT_BUTTON(ID_BUTTON_DOT_CREATE_PROP, OnButtonProp)
END_EVENT_TABLE()

DotEditorPage::DotEditorPage( wxWindow* parent )
	: wxPanel(parent)
{
	wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
	SetSizer(topSizer);

	wxStaticBoxSizer* propertySizer = new wxStaticBoxSizer(
		new wxStaticBox(this, wxID_ANY, _("Properties")), wxVERTICAL);
	topSizer->Add(propertySizer);

	// DOT COUNT
	wxBoxSizer* dotCountSizer = new wxBoxSizer(wxHORIZONTAL);
	propertySizer->Add(dotCountSizer);
	dotCountSizer->Add(
		new wxStaticText(this, wxID_ANY, _("Count : ")), wxSizerFlags().Border());
	dotCountSizer->Add(
		new wxTextCtrl(this, ID_TEXT_DOT_COUNT_X, _("15")), wxSizerFlags().Border());
	dotCountSizer->Add(
		new wxTextCtrl(this, ID_TEXT_DOT_COUNT_Y, _("15")), wxSizerFlags().Border());
	dotCountSizer->Add(
		new wxTextCtrl(this, ID_TEXT_DOT_COUNT_Z, _("15")), wxSizerFlags().Border());
	mCountX = 15;
	mCountY = 15;
	mCountZ = 15;

	// DOT SIZE
	wxBoxSizer* dotSizeSizer = new wxBoxSizer(wxHORIZONTAL);
	propertySizer->Add(dotSizeSizer);
	dotSizeSizer->Add(
		new wxStaticText(this, wxID_ANY, _("Size : ")), wxSizerFlags().Border());
	dotSizeSizer->Add(
		new wxTextCtrl(this, ID_TEXT_DOT_SIZE, _("6")), wxSizerFlags().Border());
	dotSizeSizer->Add(
		new wxStaticText(this, wxID_ANY, _("cm")), wxSizerFlags().Border());
	mDotSize = 6 * 0.01; // 6 cm

	// DOT ORIGIN
	wxBoxSizer* dotOriginSizer = new wxBoxSizer(wxHORIZONTAL);
	propertySizer->Add(dotOriginSizer);
	dotOriginSizer->Add(
		new wxStaticText(this, wxID_ANY, _("Origin : ")), wxSizerFlags().Border());
	dotOriginSizer->Add(
		new wxTextCtrl(this, ID_TEXT_DOT_ORIGIN_X, _("0")), wxSizerFlags().Border());
	dotOriginSizer->Add(
		new wxTextCtrl(this, ID_TEXT_DOT_ORIGIN_Y, _("0")), wxSizerFlags().Border());
	dotOriginSizer->Add(
		new wxTextCtrl(this, ID_TEXT_DOT_ORIGIN_Z, _("0")), wxSizerFlags().Border());
	mOriginX = 0;
	mOriginY = 0;
	mOriginZ = 0;

	// CHANGE PROPERTY BUTTON
	propertySizer->Add(
		new wxButton(this, ID_BUTTON_DOT_CHANGE_PROPERTY, _("Change Property")),
		wxSizerFlags().Border());

	wxStaticBoxSizer* creationSizer = new wxStaticBoxSizer(
		new wxStaticBox(this, wxID_ANY, _("Create")), wxVERTICAL );
	topSizer->Add(creationSizer);

	// CHARACTER CREATION BUTTON
	creationSizer->Add(
		new wxButton(this, ID_BUTTON_DOT_CREATE_CHARACTER, _("Create Character")),
		wxSizerFlags().Border());

	// PROP CREATION BUTTON
	creationSizer->Add(
		new wxButton(this, ID_BUTTON_DOT_CHANGE_PROPERTY, _("Create Prop")),
		wxSizerFlags().Border());
}

void DotEditorPage::OnButtonChangeProperty(wxCommandEvent& evt)
{
	// DOT COUNT
	bool result = RefreshDotCount();
	if (result)
	{
		wxGetApp().SetDotCount((int)mCountX, (int)mCountY, (int)mCountZ);
	}
	else
	{
		wxGetApp().Message(_("Dot count is not valid."));
	}

	// DOT SIZE
	result = RefreshDotSize();
	if (result)
	{
		wxGetApp().SetDotSize((float)mDotSize);
	}
	else
	{
		wxGetApp().Message(_("Dot size is not valid."));
	}
	
	// DOT ORIGIN
	result = RefreshOrigin();
	if (result)
	{
		wxGetApp().SetDotOrigin((int)mOriginX, (int)mOriginY, (int)mOriginZ);
	}
	else
	{
		wxGetApp().Message(_("Dot origin is not valid."));
	}
}

void DotEditorPage::OnButtonCharacter(wxCommandEvent& evt)
{
	bool saved = true;
	if (wxGetApp().NeedToSaveDotObject())
	{
		saved = Save();
	}
	if (saved)
	{
		RefreshDotCount();
		RefreshDotSize();
		RefreshOrigin();
		wxGetApp().SetupNewDotCharacter(
				(unsigned)mCountX, (unsigned)mCountY, (unsigned)mCountZ,
				(float)mDotSize,
				(int)mOriginX, (int)mOriginY, (int)mOriginZ
			);
		mFilePath.clear();
	}
}

void DotEditorPage::OnButtonProp(wxCommandEvent& evt)
{
	bool saved = true;
	if (wxGetApp().NeedToSaveDotObject())
	{
		saved = Save();
	}
	if (saved)
	{
		RefreshDotCount();
		RefreshDotSize();
		RefreshOrigin();
		wxGetApp().SetupNewDotProp(
				(unsigned)mCountX, (unsigned)mCountY, (unsigned)mCountZ,
				(float)mDotSize,
				(int)mOriginX, (int)mOriginY, (int)mOriginZ
			);
		mFilePath.clear();
	}
}

bool DotEditorPage::Save()
{
	if (mFilePath.empty())
	{
		wxString strFileFilter = _( 
			"Scene file(*.dotobject)|*.dotobject|\
			All files(*.*)|*.*"
			);

		wxString currentFilepath = _T("data/dotobjects/");

		wxFileDialog dialog(this, _("Select a dot object file."), currentFilepath, currentFilepath, strFileFilter, 
			wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
		if (dialog.ShowModal() == wxID_OK)
		{
			mFilePath = dialog.GetPath();
		}
		else
		{
			return false;
		}
	}

	wxGetApp().SaveDotObject(mFilePath.mb_str());
	wxGetApp().SetMainFrameTitle( wxString::Format(_("OgreGameTool - %s"), mFilePath.c_str()) );
	return true;
}

bool DotEditorPage::SaveAs()
{
	wxString strFileFilter = _( 
		"Scene file(*.dotobject)|*.dotobject|\
		All files(*.*)|*.*"
		);

	wxString currentFilepath = _T("data/dotobjects");

	wxFileDialog dialog(this, _("Select a dot object file."), currentFilepath, currentFilepath, strFileFilter, 
		wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
	if (dialog.ShowModal() == wxID_OK)
	{
		mFilePath = dialog.GetPath();
	}
	else
	{
		return false;
	}

	wxGetApp().SaveDotObject(mFilePath.mb_str());
	wxGetApp().SetMainFrameTitle( wxString::Format(_("OgreGameTool - %s"), mFilePath.c_str()) );
	return true;
}

bool DotEditorPage::RefreshDotCount()
{
	wxTextCtrl* pTextCtrl = dynamic_cast<wxTextCtrl*>(FindWindow(ID_TEXT_DOT_COUNT_X));
	assert(pTextCtrl);
	bool result = true;
	result &= pTextCtrl->GetValue().ToULong(&mCountX);
	pTextCtrl = dynamic_cast<wxTextCtrl*>(FindWindow(ID_TEXT_DOT_COUNT_Y));
	assert(pTextCtrl);
	result &= pTextCtrl->GetValue().ToULong(&mCountY);
	pTextCtrl = dynamic_cast<wxTextCtrl*>(FindWindow(ID_TEXT_DOT_COUNT_Z));
	assert(pTextCtrl);
	result &= pTextCtrl->GetValue().ToULong(&mCountZ);
	if (mCountX % 2 == 0)
	{
		mCountX+=1;
	}
	if (mCountY % 2 == 0)
	{
		mCountY+=1;
	}
	if (mCountZ % 2 == 0)
	{
		mCountZ+=1;
	}
	return result;
}

bool DotEditorPage::RefreshDotSize()
{
	wxTextCtrl* pTextCtrl = dynamic_cast<wxTextCtrl*>(FindWindow(ID_TEXT_DOT_SIZE));
	assert(pTextCtrl);
	bool result = pTextCtrl->GetValue().ToDouble(&mDotSize);
	result &= mDotSize > 0.0;
	return result;
}

bool DotEditorPage::RefreshOrigin()
{
	wxTextCtrl* pTextCtrl = dynamic_cast<wxTextCtrl*>(FindWindow(ID_TEXT_DOT_ORIGIN_X));
	assert(pTextCtrl);
	bool result = true;
	result &= pTextCtrl->GetValue().ToLong(&mOriginX);
	pTextCtrl = dynamic_cast<wxTextCtrl*>(FindWindow(ID_TEXT_DOT_ORIGIN_X));
	assert(pTextCtrl);
	result &= pTextCtrl->GetValue().ToLong(&mOriginY);
	pTextCtrl = dynamic_cast<wxTextCtrl*>(FindWindow(ID_TEXT_DOT_ORIGIN_X));
	assert(pTextCtrl);
	result &= pTextCtrl->GetValue().ToLong(&mOriginZ);
	return result;
}